package com.studentgrowth.controller.learning;

import com.studentgrowth.dto.learningplan.LearningPlanResponse;
import com.studentgrowth.dto.learningplan.QuestionnaireRequest;
import com.studentgrowth.service.learning.LearningPlanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/learning-plan")
public class LearningPlanController {
    
    private static final Logger log = LoggerFactory.getLogger(LearningPlanController.class);

    @Autowired
    private LearningPlanService learningPlanService;

    @PostMapping("/generate")
    public ResponseEntity<LearningPlanResponse> generatePlan(
            @RequestBody QuestionnaireRequest request,
            @RequestHeader("Authorization") String token) {
        log.debug("收到生成计划请求: {}", request);
        try {
            Long userId = getUserIdFromToken(token);
            LearningPlanResponse plan = learningPlanService.generatePlan(request, userId);
            log.debug("生成计划成功: {}", plan);
            return ResponseEntity.ok(plan);
        } catch (Exception e) {
            log.error("生成计划失败", e);
            throw new RuntimeException("生成计划失败: " + e.getMessage());
        }
    }

    // 从token中获取用户ID
    private Long getUserIdFromToken(String token) {
        // 暂时返回固定值，等JWT服务完成后再实现真实逻辑
        return 1L;
        
        /*
        // JWT解析代码，暂时注释
        if (token != null && token.startsWith("Bearer ")) {
            String jwt = token.substring(7);
            Claims claims = Jwts.parser()
                .setSigningKey(jwtSecret)
                .parseClaimsJws(jwt)
                .getBody();
            return Long.parseLong(claims.getSubject());
        }
        throw new RuntimeException("Invalid token");
        */
    }
} 